package speelotheek.views;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;

import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import speelotheek.models.Overzicht;
import speelotheek.models.Lid;

import javax.swing.ListSelectionModel;

public class Customers extends JPanel
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 7730124103956917040L;
	private JTextField textField;
	private JTable table;
	
	/**
	 * Create the panel.
	 */
	public Customers() 
	{
		JPanel panelFilter = new JPanel();
		panelFilter.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Filter:", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		JPanel panelTable = new JPanel();
		panelTable.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Leden: ", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		JPanel panelButtons = new JPanel();
		panelButtons.setBorder(new TitledBorder(null, "Met Geselecteerd:", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		JPanel panelMore = new JPanel();
		panelMore.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Meer:", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(panelTable, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 781, Short.MAX_VALUE)
						.addComponent(panelFilter, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 781, Short.MAX_VALUE)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(panelButtons, GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE)
							.addGap(18)
							.addComponent(panelMore, GroupLayout.PREFERRED_SIZE, 441, Short.MAX_VALUE)))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(panelFilter, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(panelTable, GroupLayout.DEFAULT_SIZE, 449, Short.MAX_VALUE)
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(panelButtons, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)
						.addComponent(panelMore, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE))
					.addContainerGap())
		);
		
		JButton btnStatistieken = new JButton("Statistieken");
		btnStatistieken.setIcon(new ImageIcon(Products.class.getResource("/speelotheek/resources/globe_48.png")));
		btnStatistieken.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				final Statistics stats = new Statistics();
				stats.setVisible(true);
				stats.addWindowListener(new WindowAdapter()
			    {
			      public void windowClosed(WindowEvent e)
			      {
			        stats.removeWindowListener(this);
			      }
			    });
			}
		});
		btnStatistieken.setIcon(new ImageIcon(Customers.class.getResource("/speelotheek/resources/globe_48.png")));
		
		JButton btnNieuwLid = new JButton("Lid Toevoegen");
		btnNieuwLid.setIcon(new ImageIcon(Customers.class.getResource("/speelotheek/resources/add_48.png")));
		btnNieuwLid.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				final AddCustomer addCustomer = new AddCustomer();
				addCustomer.setVisible(true);
				addCustomer.addWindowListener(new WindowAdapter() {
					public void windowClosed(WindowEvent e) {
						reloadMembers();
						addCustomer.removeWindowListener(this);
					}
				});
			}
		});
		
		GroupLayout gl_panelMore = new GroupLayout(panelMore);
		gl_panelMore.setHorizontalGroup(
			gl_panelMore.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelMore.createSequentialGroup()
					.addContainerGap()
					.addComponent(btnStatistieken)
					.addGap(18)
					.addComponent(btnNieuwLid)
					.addContainerGap(171, Short.MAX_VALUE))
		);
		gl_panelMore.setVerticalGroup(
			gl_panelMore.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelMore.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panelMore.createParallelGroup(Alignment.LEADING)
						.addComponent(btnNieuwLid)
						.addComponent(btnStatistieken))
					.addContainerGap(19, Short.MAX_VALUE))
		);
		panelMore.setLayout(gl_panelMore);
					
		JButton btnMeerInfo = new JButton("Meer Info");
		btnMeerInfo.setIcon(new ImageIcon(Products.class.getResource("/speelotheek/resources/info_48.png")));
		btnMeerInfo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (table.getSelectedRow() > -1)
				{
					String nummer = table.getValueAt(table.getSelectedRow(), 0).toString();
					
					final CustomerInfo customerInfo = new CustomerInfo(nummer);
					customerInfo.setVisible(true);
					customerInfo.addWindowListener(new WindowAdapter() {
						public void windowClosed(WindowEvent e) {
							reloadMembers();
							customerInfo.removeWindowListener(this);
						}
					});					
				}
				else {
					JOptionPane.showMessageDialog(null, "Geen lid geselecteerd");
				}
			}
		});
		
		JButton btnVerwijderen = new JButton("Verwijderen");
		btnVerwijderen.setIcon(new ImageIcon(Customers.class.getResource("/speelotheek/resources/delete_48.png")));
		btnVerwijderen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (table.getSelectedRow() > -1) {
					String nummer = table.getValueAt(table.getSelectedRow(), 0).toString();
					String msg = "Weet u zeker dat u dit lid uit de database wilt verwijderen?\n\nLet op: Uitleningen gekoppeld aan dit lid zullen ook verloren gaan.";
					int confirmRemove = JOptionPane.showConfirmDialog(null, msg, "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
				    
					switch(confirmRemove) {
						case JOptionPane.YES_OPTION:							
							if(Lid.removeCustomer(nummer)){
								reloadMembers();
							} 
							else {
								JOptionPane.showMessageDialog(null, "Er is iets fout gegaan bij het verwijderen van het lid.\n Neem contact op met de beheerder.", "Error", JOptionPane.ERROR_MESSAGE);
							}							
						break;
					}
				} else {
					JOptionPane.showMessageDialog(null, "Geen lid geselecteerd");
				}
			}
		});
		
		GroupLayout gl_panelButtons = new GroupLayout(panelButtons);
		gl_panelButtons.setHorizontalGroup(
			gl_panelButtons.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelButtons.createSequentialGroup()
					.addContainerGap()
					.addComponent(btnMeerInfo)
					.addGap(18)
					.addComponent(btnVerwijderen)
					.addContainerGap(640, Short.MAX_VALUE))
		);
		gl_panelButtons.setVerticalGroup(
			gl_panelButtons.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelButtons.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panelButtons.createParallelGroup(Alignment.LEADING)
						.addComponent(btnVerwijderen)
						.addComponent(btnMeerInfo))
					.addContainerGap(23, Short.MAX_VALUE))
		);
		panelButtons.setLayout(gl_panelButtons);
		
		textField = new JTextField();
		textField.setColumns(10);
		
		JButton btnZoeken = new JButton("Zoeken");
		btnZoeken.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				Overzicht members = new Overzicht();
				members.setType("member");
				String search = textField.getText();
				members.setSearch(search);
				members.createVectors();
				
				Vector data = members.getData();
				Vector columnNames = members.getColumns();
				DefaultTableModel dataModel = new DefaultTableModel(data, columnNames);
				table.setModel(dataModel);
				table.repaint();
			}
		});
		
		JButton btnResetFilter = new JButton("Reset Filter");
		btnResetFilter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {				
				Overzicht members = new Overzicht();
				members.setType("member");
				members.createVectors();
				
				//Clear user input
				textField.setText("");
				
				Vector data = members.getData();
				Vector columnNames = members.getColumns();
				DefaultTableModel dataModel = new DefaultTableModel(data, columnNames);
				table.setModel(dataModel);
				table.repaint();
			}
		});
		
		GroupLayout gl_panelFilter = new GroupLayout(panelFilter);
		gl_panelFilter.setHorizontalGroup(
			gl_panelFilter.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelFilter.createSequentialGroup()
					.addContainerGap()
					.addComponent(textField, GroupLayout.PREFERRED_SIZE, 158, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(btnZoeken)
					.addPreferredGap(ComponentPlacement.RELATED, 589, Short.MAX_VALUE)
					.addComponent(btnResetFilter)
					.addContainerGap())
		);
		gl_panelFilter.setVerticalGroup(
			gl_panelFilter.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelFilter.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panelFilter.createParallelGroup(Alignment.BASELINE)
						.addComponent(textField, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnZoeken)
						.addComponent(btnResetFilter))
					.addContainerGap(53, Short.MAX_VALUE))
		);
		panelFilter.setLayout(gl_panelFilter);
		
		setLayout(groupLayout);
		
		Overzicht members = new Overzicht();
		members.setType("member");
		members.createVectors();
		
		Vector data = members.getData();
		Vector columnNames = members.getColumns();
		
		table = new JTable(data, columnNames)
        {
            public boolean isCellEditable(int row, int col)
            {
                return false;
            }
        };		
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setAutoCreateRowSorter(true);
		table.setRowHeight(20);
		table.setFont(new Font("Tahoma", Font.PLAIN, 12));
		table.setShowVerticalLines(false);
		table.setForeground(Color.BLACK);
		table.setBackground(Color.WHITE);
		JScrollPane scrollPane = new JScrollPane(table);
		table.setFillsViewportHeight(true);
		GroupLayout gl_panelTable = new GroupLayout(panelTable);
		gl_panelTable.setHorizontalGroup(
			gl_panelTable.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelTable.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 417, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_panelTable.setVerticalGroup(
			gl_panelTable.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_panelTable.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 393, Short.MAX_VALUE)
					.addContainerGap())
		);
		panelTable.setLayout(gl_panelTable);
	}
	
	public void reloadMembers()
	{		
		Overzicht members = new Overzicht();
		members.setType("member");
		
		String search = textField.getText();
		members.setSearch(search);
		
		members.createVectors();
		
		Vector data = members.getData();
		Vector columnNames = members.getColumns();
		DefaultTableModel dataModel = new DefaultTableModel(data, columnNames);
		table.setModel(dataModel);
		table.repaint();			
	}
}
